﻿@using Webcar.BLL
@helper CreateQuery(Action<QuerySettings> action)
{
    QuerySettings settings = new QuerySettings();
    action(settings);
    UserQueryBuilder.ClearQuery(settings.QueryKey);
    <hr />
    <table style="margin-left:2px;margin-bottom:0px;margin-top:0px;">
        <tr style="vertical-align:middle;">
            <td>
                <select id="query_select1" name="query_select1" style="width: 150px; float: left;" onchange="retriveResult1();">
                    @foreach (var item in settings.Columns)
                    { 
                        <option value="@item.Field;@item.DataType" data_type="@item.DataType" action="@item.CallBackAction">@item.Name</option>
                    }
                </select>
            </td>
            <td>
                <select id="query_operator1" name="query_operator1" style="float: left;">
                    <option value="contains">包含</option>
                    <option value="equals">等于</option>
                    <option value="not_equals">不等于</option>
                    <option value="greater">大于</option>
                    <option value="less">小于</option>
                </select>
            </td>
            <td>
                <div id="query_result_area1" style="float: left">
                    <input id="query_result1" name="query_result1" type="text" />

                </div>
            </td>
            <td>
                <select id="query_andor" name="query_andor" style="float: left;">
                    <option value="operator_and">并且</option>
                    <option value="operator_or">或者</option>
                </select>
            </td>
            <td>
                <select id="query_select2" name="query_select2" style="width: 150px; float: left" onchange="retriveResult2();">
                    @foreach (var item in settings.Columns)
                    { 
                        <option value="@item.Field;@item.DataType" data_type="@item.DataType" action="@item.CallBackAction">@item.Name</option>
                    }
                </select>
            </td>
            <td>
                <select id="query_operator2" name="query_operator2" style="float: left">
                    <option value="equals">等于</option>
                    <option value="contains">包含</option>
                    <option value="not_equals">不等于</option>
                    <option value="greater">大于</option>
                    <option value="less">小于</option>
                </select>
            </td>

            <td>
                <div id="query_result_area2" style="float: left">
                    <input id="query_result2" name="query_operator2" type='text' />
                </div>
            </td>
            <td>
                <input id="btnQuery" type="submit" class="webcar-button" value="查询" />
            </td>
        </tr>
    </table>
    <hr />
    <script type="text/javascript">
        retriveResult1();
        retriveResult2();

        function retriveResult1() {
            loadResultContent("#query_select1", "#query_result_area1", "#query_operator1", "query_result1");
        };
        function retriveResult2() {
            loadResultContent("#query_select2", "#query_result_area2", "#query_operator2", "query_result2");
        };


        function loadResultContent(source, target,operator, result_name) {
            var select = $(source);
            var action = select.find("option:selected").attr("action");
            var data_type = select.find("option:selected").attr("data_type");

            if (action == undefined || action == null || action == "") {
                $(target).html("<input id='" + result_name + "' name='"+result_name+"' type='text' />");
            }
            else {
                $(target).load(action + "?name=" + result_name);
            }
            
            $(operator).empty();
            if (data_type == "Text") {
                $(operator).html(
                    "<option value=\"contains\">包含</option>" +
                    "<option value=\"equals\">等于</option>"+
                    "<option value=\"not_equals\">不等于</option>"
                    );
            }
            if (data_type == "Number") {
                $(operator).html(
                    "<option value=\"equals\">等于</option>" +
                    "<option value=\"not_equals\">不等于</option>" +
                    "<option value=\"greater\">大于</option>" +
                    "<option value=\"less\">小于</option>"
                    );
            }
            if (data_type == "Date") {
                $(operator).html(
                    "<option value=\"equals\">等于</option>" +
                    "<option value=\"not_equals\">不等于</option>" +
                    "<option value=\"greater\">大于</option>" +
                    "<option value=\"less\">小于</option>"
                    );
            }
            if (data_type == "DropDownList") {
                $(operator).html(
                    "<option value=\"equals\">等于</option>"
                    );
            }

        };
    </script>
    }

